package com.leyou.item.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.TbSpecGroup;
import com.leyou.item.entity.TbSpecParam;
import com.leyou.item.mapper.TbSpecGroupMapper;
import com.leyou.item.service.TbSpecGroupService;

import com.leyou.item.service.TbSpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 规格参数的分组表，每个商品分类下有多个规格参数组 服务实现类
 * </p>
 *
 * @author mybaitsplus
 * @since 2019-10-18
 */
@Service
public class TbSpecGroupServiceImpl extends ServiceImpl<TbSpecGroupMapper, TbSpecGroup> implements TbSpecGroupService {
    @Autowired
    private TbSpecParamService specParamService;
    @Override
    public List<SpecGroupDTO> findSpecGroupByCategoryId(Long categoryId) {

        QueryWrapper<TbSpecGroup> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(TbSpecGroup::getCid,categoryId);
        List<TbSpecGroup> list = this.list(queryWrapper);
        if(CollectionUtils.isEmpty(list)){
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, SpecGroupDTO.class);
    }

    @Override
    public List<SpecParamDTO> findSpecParamByGroupId(Long groupId,Long cid,Boolean searching) {
        QueryWrapper<TbSpecParam> queryWrapper= new QueryWrapper<>();
        if (groupId!=null&&groupId!=0)
        queryWrapper.lambda().eq(TbSpecParam::getGroupId,groupId);
        if(cid!=null&&cid!=0)
            queryWrapper.lambda().eq(TbSpecParam::getCid,cid);
        if (searching!=null)
            queryWrapper.lambda().eq(TbSpecParam::getSearching,searching);
        List<TbSpecParam> list = specParamService.list(queryWrapper);
        if(CollectionUtils.isEmpty(list)){
            throw new LyException(ExceptionEnum.SPEC_PARAM_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list,SpecParamDTO.class);
    }

    @Override
    @Transactional//事物
    public void addGroup(TbSpecGroup group) {
        if(StringUtils.isEmpty(group.getName()))
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        boolean save = this.save(group);
        if(!save)
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
    }

    @Override
    @Transactional//事物
    public void updateGroup(TbSpecGroup group) {
        if(StringUtils.isEmpty(group.getName()))
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        boolean b = this.updateById(group);
        if (!b)
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
    }

    @Override
    @Transactional//事物
    public void deleteGroupById(String id) {
        if(StringUtils.isEmpty(id))
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        boolean b = this.removeById(id);
        if (!b)
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        QueryWrapper<TbSpecParam> queryWrapper =new QueryWrapper<>();
        queryWrapper.lambda().eq(TbSpecParam::getGroupId,id);
        boolean remove = specParamService.remove(queryWrapper);
        if (!remove)
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
    }
}
